package com.uphone.kingmall.activity.order;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.adapter.RvEvaluateAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.EvaluateBean;
import com.uphone.kingmall.bean.GoodsComment;
import com.uphone.kingmall.bean.OrderBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PictureSelectUtils;
import com.uphone.kingmall.utils.RatingBar;
import com.uphone.kingmall.utils.intent.IntentData;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/27
 *     desc   : 订单评价
 *     version: 1.0
 * </pre>
 */
public class OrderEvaluateActivity extends BaseActivity {
    @BindView(R.id.rb_shop)
    RatingBar rbShop;
    @BindView(R.id.rv_order_evaluate)
    RecyclerView rvOrderEvaluate;
    @IntentData
    private OrderBean.DataBean bean;
    private int selPosition = -1;

    private List<List<LocalMedia>> localMediaListList = new ArrayList<>();
    private List<EvaluateBean> evaluateBeanList = new ArrayList<>();
    private RvEvaluateAdapter evaluateAdapter;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_order_evaluate;
    }

    @Override
    public void initView() {
        setStatusBar();
        if (bean == null) {
            return;
        }
        for (OrderBean.DataBean.GoodslistBean goodsBean : bean.getGoodslist()) {
            List<LocalMedia> localMediaList = new ArrayList<>();

//            LocalMedia localMedia = new LocalMedia();
//            localMedia.setPath("upload");
//            localMediaList.add(localMedia);

            localMediaListList.add(localMediaList);

            EvaluateBean evaluateBean = new EvaluateBean();
            evaluateBean.setContent("");
            evaluateBean.setGoodsId(goodsBean.getOrderGoodsId() + "");
            evaluateBean.setImageUrl(goodsBean.getGoodsImg());
            evaluateBean.setRating(0);
            evaluateBean.setLocalMediaList(localMediaList);
            evaluateBeanList.add(evaluateBean);
        }

        evaluateAdapter = new RvEvaluateAdapter(this, evaluateBeanList, new RvEvaluateAdapter.GoodsEvaluateChange() {
            @Override
            public void ratingChange(int poisition, double rating) {
                evaluateBeanList.get(poisition).setRating(rating);
                LogUtils.i("评分更新后=" + evaluateBeanList.toString());
            }

            @Override
            public void evaluateChange(int poisition, String evaluate) {
                evaluateBeanList.get(poisition).setContent(evaluate);
                LogUtils.i("评价更新后=" + evaluateBeanList.toString());
            }

            @Override
            public void imageChange(int poisition) {
                selPosition = poisition;
                List<LocalMedia> localMediaList = evaluateBeanList.get(poisition).getLocalMediaList();
                PictureSelectUtils.openMultiplePicture(OrderEvaluateActivity.this, localMediaList,
                        6, false, 0, 0, PictureConfig.CHOOSE_REQUEST);
            }
        });
        rvOrderEvaluate.setLayoutManager(new LinearLayoutManager(this));
        rvOrderEvaluate.setAdapter(evaluateAdapter);
        rvOrderEvaluate.setNestedScrollingEnabled(false);

        rbShop.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {
            }
        });
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.iv_back, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_submit:
                submit();
                break;
            default:
                break;
        }
    }

    private void submit() {
        HttpParams params = new HttpParams();
        params.put("orderId", bean.getOrderId());
        params.put("shopScore", (int) rbShop.getStart());
        List<File> files = new ArrayList<>();
        for (int i = 0; i < localMediaListList.size(); i++) {
            for (int j = 0; j < localMediaListList.get(i).size(); j++) {
                String path = localMediaListList.get(i).get(j).getPath();
                if (!TextUtils.isEmpty(path)) {
                    files.add(new File(path));
                }
            }

        }
        params.putFileParams("commImgs", files);
        List<GoodsComment> comments = new ArrayList<>();
        for (int i = 0; i < evaluateBeanList.size(); i++) {
            EvaluateBean evaluateBean = evaluateBeanList.get(i);
            GoodsComment comment = new GoodsComment();
            comment.setComment(evaluateBean.getContent());
            comment.setGoodsScore(((int) evaluateBean.getRating()) + "");
            comment.setImgNum(evaluateBean.getLocalMediaList().size() + "");
            comment.setOrderGoodsId(evaluateBean.getGoodsId() + "");
            comments.add(comment);
        }
        params.put("goodsComments", GsonUtils.getGson().toJson(comments));
        OkGoUtils.progressRequest(MyUrl.orderComment, this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    CommonUtil.startIntent(OrderEvaluateActivity.this, EvaluateSuccessActivity.class, bean.getOrderId());
                    finish();
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
//                    LocalMedia localMedia = new LocalMedia();
//                    localMedia.setPath("upload");
//                    selectList.add(0,localMedia);
                    localMediaListList.set(selPosition, selectList);
                    evaluateBeanList.get(selPosition).setLocalMediaList(selectList);
                    evaluateAdapter.setNewData(evaluateBeanList);
                    break;
                default:
                    break;
            }
        }
    }

}
